package com.blockmeta.bbs.businesslibrary.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil
import com.blockmeta.bbs.businesslibrary.R
import java.text.DecimalFormat

/**
 * Created by AirThor
 * Date 2018/4/27.
 * F10简介 百分比图
 */
class PercentView : View {
    //百分比
    var percent: Double =0.0
    //底色
    var colorUnder: Int =0
        set(value) {
            field=value
            underRectPaint.color=value
        }
    //百分比颜色
    var colorAbove: Int =0
        set(value) {
            field=value
            aboveRectPaint.color=value
        }
    //stroke颜色
    var colorStroke: Int =0
        set(value) {
            field = value
            strokeRectPaint.color = value
        }
    //是否有stroke
    var isHaveStroke = false
    //是否不画出百分比
    var isNoText = false
    //百分比文字的位置
    var percentGravity = 0
    //文字大小
    var textSize: Float=DimentionsUtil.dp2px(context,16f).toFloat()
    //排名
    var ranking: String =""
    //圆角半径
    var cornerRadius: Float=DimentionsUtil.dp2px(context,4f).toFloat()
    //图的总长度
    private var totalWidth: Float=0f
    //文字margin值
    private val margin = DimentionsUtil.dp2px(context,10f).toFloat()

    //文字Y轴坐标
    private var yAxis =0f


    //文字画笔
    val textPaint= Paint(Paint.ANTI_ALIAS_FLAG)
    //柱状图画笔
    val underRectPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    val aboveRectPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    val strokeRectPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    var mStartContent: String = ""

    private val strokeWidth = DimentionsUtil.dp2px(context,1f).toFloat()

    constructor(context: Context?) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.PercentView)

        colorUnder=typedArray.getColor(R.styleable.PercentView_underColor,Color.GREEN)
        colorAbove=typedArray.getColor(R.styleable.PercentView_aboveColor,Color.BLUE)
        colorStroke = typedArray.getColor(R.styleable.PercentView_strokeColor,Color.GRAY)
        isHaveStroke = typedArray.getBoolean(R.styleable.PercentView_stroke,false)
        isNoText = typedArray.getBoolean(R.styleable.PercentView_noText,false)
        percentGravity = typedArray.getInt(R.styleable.PercentView_percentGravity,0)
        cornerRadius= typedArray.getDimension(R.styleable.PercentView_cornerRadius,DimentionsUtil.dp2px(context,4f).toFloat())
        textSize = typedArray.getDimension(R.styleable.PercentView_cornerRadius,DimentionsUtil.dp2px(context,16f).toFloat())
        val textColor = typedArray.getColor(R.styleable.PercentView_textColor,Color.WHITE)

        typedArray.recycle()

        underRectPaint.color = colorUnder
        aboveRectPaint.color = colorAbove
        strokeRectPaint.color = colorStroke
        textPaint.textSize = textSize
        textPaint.color = textColor
    }

    init {
        textPaint.typeface= Typeface.createFromAsset(context.assets,"fonts/DIN-Medium.otf")
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        totalWidth=if (isHaveStroke){
            w.toFloat()
        }else{
            w.toFloat()
        }
        if (cornerRadius>h/2) cornerRadius=h/2f
        yAxis=(h-(textPaint.fontMetrics.bottom-textPaint.fontMetrics.top))/2-textPaint.fontMetrics.top
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (isHaveStroke){
            drawStroke(canvas)
        }

        drawUnderRect(canvas)

        drawAboveRect(canvas)

        if (!isNoText) {
            drawText(canvas)
        }
    }

    private fun drawText(canvas: Canvas) {
        if (percentGravity == 0) {
            textPaint.textAlign = Paint.Align.LEFT
            val percentStr = if (mStartContent.isEmpty()){DecimalFormat("#0.00%").format(percent)}else{mStartContent}
            canvas.drawText(percentStr, margin, yAxis, textPaint)
        }else if (percentGravity == 1){
            textPaint.textAlign = Paint.Align.CENTER
            val percentStr = if (mStartContent.isEmpty()){DecimalFormat("#0.00%").format(percent)}else{mStartContent}
            canvas.drawText(percentStr, measuredWidth/2f, yAxis, textPaint)
        }

        if (ranking.isNotEmpty())
            textPaint.textAlign=Paint.Align.RIGHT
            canvas.drawText(ranking,measuredWidth-margin,yAxis,textPaint)
    }

    private fun drawAboveRect(canvas: Canvas) {
        canvas.save()
        val path = Path()
        if (isHaveStroke){
            val innerRadius = cornerRadius - strokeWidth
            path.moveTo(strokeWidth,innerRadius)
            val topArcRect = RectF(strokeWidth,strokeWidth,innerRadius*2,innerRadius*2)
            path.addArc(topArcRect,180f,90f)
            path.lineTo(measuredWidth.toFloat()-strokeWidth-innerRadius,strokeWidth)
            val topRightRect = RectF(measuredWidth-innerRadius*2-strokeWidth,strokeWidth,measuredWidth-strokeWidth,innerRadius*2+strokeWidth)
            path.arcTo(topRightRect,270f,90f)
            path.lineTo(measuredWidth-strokeWidth,measuredHeight-innerRadius-strokeWidth)
            val bottomRightRect = RectF(measuredWidth-innerRadius*2-strokeWidth,measuredHeight-strokeWidth-innerRadius*2,measuredWidth-strokeWidth,measuredHeight-strokeWidth)
            path.arcTo(bottomRightRect,0f,90f)
            path.lineTo(innerRadius+strokeWidth,measuredHeight.toFloat()-strokeWidth)
            val bottomArcRect = RectF(strokeWidth,measuredHeight-innerRadius*2-strokeWidth,innerRadius*2+strokeWidth,measuredHeight.toFloat()-strokeWidth)
            path.arcTo(bottomArcRect,90f,90f)
            path.close()
            canvas.clipPath(path)
        }else{
            path.moveTo(0f,cornerRadius)
            val topArcRect = RectF(0f,0f,cornerRadius*2,cornerRadius*2)
            path.addArc(topArcRect,180f,90f)
            path.lineTo(measuredWidth.toFloat(),0f)
            path.lineTo(measuredWidth.toFloat(),measuredHeight.toFloat())
            path.lineTo(cornerRadius,measuredHeight.toFloat())
            val bottomArcRect = RectF(0f,measuredHeight-cornerRadius*2,cornerRadius*2,measuredHeight.toFloat())
            path.arcTo(bottomArcRect,90f,90f)
            path.close()
            canvas.clipPath(path)
        }

        val right = (totalWidth*percent).toFloat()
        val rectF = if (isHaveStroke){
            RectF(strokeWidth,strokeWidth, (totalWidth*percent).toFloat(),measuredHeight.toFloat()-strokeWidth)
        }else{
            RectF(0f,0f, (totalWidth*percent).toFloat(),measuredHeight.toFloat())
        }
        if (right<cornerRadius*2f||isHaveStroke){
            canvas.drawRect(rectF,aboveRectPaint)
        }else {
            canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, aboveRectPaint)
        }
        canvas.restore()
    }

    private fun drawUnderRect(canvas: Canvas) {
        var rectF = if(isHaveStroke) {
            RectF(strokeWidth, strokeWidth, measuredWidth.toFloat()-strokeWidth, measuredHeight.toFloat()-strokeWidth)
        }else{
            RectF(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
        }
        canvas.drawRoundRect(rectF,cornerRadius,cornerRadius,underRectPaint)
    }

    private fun drawStroke(canvas: Canvas) {
        val rectF=RectF(0f,0f,measuredWidth.toFloat(),measuredHeight.toFloat())
        canvas.drawRoundRect(rectF,cornerRadius,cornerRadius,strokeRectPaint)
    }
}